package com.glory.brepda.activity.lanjian;

import android.app.Dialog;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;

import com.glory.brepda.adapter.CheckBoxListAdapter;
import com.glory.brepda.adapter.CheckReelListAdapter;
import com.glory.brepda.adapter.PmsEqpHisListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.glory.brepda.utils.ShowDialog;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class A048CheckBoxReelActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvClear)
    TextView tvClear;
    @BindView(R.id.reel)
    EditText reel;
    @BindView(R.id.box)
    EditText box;
    @BindView(R.id.rvcheckReelList)
    RecyclerView rvcheckReelList;
    @BindView(R.id.rvcheckBoxList)
    RecyclerView rvcheckBoxList;

    private CheckReelListAdapter mAdapter;
    private CheckBoxListAdapter mAdapterBox;

//    private MTextWatcher textwatcher,textwatcher2;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_a048_check_box_reel;
    }

    @Override
    protected void initView() {

        tvBaseSubmit.setText("清空数据");
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        //reel
        initRecyclerView(rvcheckReelList,this);
        mAdapter = new CheckReelListAdapter(null);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvcheckReelList.setAdapter(mAdapter);
        // box
        initRecyclerView(rvcheckBoxList,this);
        mAdapterBox = new CheckBoxListAdapter(null);
        mAdapterBox.isFirstOnly(true);
        mAdapterBox.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvcheckBoxList.setAdapter(mAdapterBox);
//        textwatcher = new MTextWatcher(reel);
//        textwatcher2 = new MTextWatcher(box);
//        reel.addTextChangedListener(textwatcher);
//        box.addTextChangedListener(textwatcher2);

        reel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                Log.d("aaaaaaa",charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = editable.toString();
                if(!text.contains(";") && StringUtils.isNotEmpty(text)){
                    showToast("请检查扫描的二维码是否是A048标签上!", false);
                    reel.setText("");
                    return;
                }
                String[] splits = text.split(";");
                String date = splits[splits.length-1];
                if(!text.isEmpty() && splits.length>5){
                    reel.setText("");
                    mAdapter.addData(date);
                }
            }
        });

        box.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                Log.d("aaaaaaa",charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = editable.toString();
                if(!text.contains(";") && StringUtils.isNotEmpty(text)){
                    showToast("请检查扫描的二维码是否是A048标签上!", false);
                    box.setText("");
                    return;
                }
                String[] splits = text.split(";");
                String date = splits[splits.length-1];
                if(!text.isEmpty() && splits.length>5){
                    box.setText("");
                    mAdapterBox.addData(date);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                mAdapterBox.getData().clear();
                mAdapterBox.notifyDataSetChanged();
                break;
        }
    }

//    private class MTextWatcher implements TextWatcher {
//
//        private EditText name;      // 通过成员变量知道我现在操作哪一个编辑框
//
//        MTextWatcher(EditText name){
//            this.name = name;
//        }
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            if (start == 0 && before == 0 && count > 1) {    //扫描 start before count = 0 0  CharSequence 为识别的码信息
//
//                if(!"".equals(this.name.getText())){
//                    Log.d("ssssssssssssssss",this.name.getText().toString());
//                }
//            } else {
//                // 为手动输入触发的事件.
//                System.out.println("手动输入触发");
//            }
//        }
//
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            System.out.println("信息改变前"+s);
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//
//            // 148015;1.2.08.06.10004;B2352148015X;20231226;V1B23521480150000784;3000
//            // 148015;1.2.08.06.10004;B2412148015X;20240318;V1B24121480150000318;6000
//            System.out.println("扫描信息"+s);
//            String[] splits = s.toString().split(";");
//            String num = splits[splits.length-1];
//            if(name.getId() == R.id.reel){
//                if(num.equals("6000")){
//                    ShowDialog.showDialog(A048CheckBoxReelActivity.this,"扫描的盘中出现了盒标签，清确认！","A048盘盒校验");
//                }
//            }else{
//                if(num.equals("3000")){
//                    ShowDialog.showDialog(A048CheckBoxReelActivity.this,"扫描的盒中出现了盘标签，清确认！","A048盘盒校验");
//                }
//            }
//            name.setText("");
//        }
//    }
}